#coding:UTF-8

import requests
import os
import time
from xpinyin import Pinyin
from lxml import etree
import json
import re

class Qianku:
    def __init__(self):
        self.headers = {
                           "Accept": "application/json, text/javascript, */*; q=0.01",
                           "X-Requested-With": "XMLHttpRequest",
                           "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
                           "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
                       }
        self.urls = ['http://bpic.588ku.com//back_water_img/19/11/20/10453278c99c6c0a736154d775ccd02036.jpg!/fw/750/quality/99/unsharp/true/compress/true']

    def create_dir(self, keyword):
        self.dir_name = '/home/syy/音乐/'+keyword
        if not os.path.exists(self.dir_name):
            os.mkdir(self.dir_name)

    def setting_param(self):
        file = input("请输入图片保存文件夹名称：")
        if file == '':
            os.exit()
        self.create_dir(file)



    def parse_url(self, url):
        response = requests.get(url, headers = self.headers)
        return response

    def save(self):
        for url in self.urls:
            response = self.parse_url(url)
            if response.status_code == 200:
                print('正在下载图片：' +"_" + url)
                down_img_name = self.dir_name+'/_{}.jpg'.format(int(time.time()))
                f= open(down_img_name, "wb")
                f.write(response.content)

if __name__ == '__main__':
    qianku = Qianku()
    qianku.setting_param()
    qianku.save()
